<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="coinfaces">

        <title>设置Pelican文章分类</title>

    <link rel="stylesheet" href="../../theme/css/style.css">
    <link rel="stylesheet" href="../../theme/css/pygments.css">



</head>

<body>
    <div class="content">
<h1>设置Pelican文章分类</h1>    <p>
        under
            <a href="../../tags/blog/">blog</a>
            <a href="../../tags/pelican/">pelican</a>
    </p>
    <p>
        in <a href="../../categories/tech/">tech</a>
    </p>
    <p>Published: 2016-09-18</p>


    <p>我一直将content目录下的第一级目录用作文章分类，以前这一级目录下就直接是reStructuredText文档，工作得很好，后来设了多级目录：</p>
<div class="highlight"><pre><span></span><span class="n">PATH_METADATA</span> <span class="o">=</span> <span class="s1">&#39;(?P&lt;category&gt;[^/]*)/(?P&lt;date&gt;\d</span><span class="si">{8}</span><span class="s1">)/(?P&lt;slug&gt;[^/]*)/(?P&lt;lang&gt;\w*)\.rst&#39;</span>
</pre></div>
<p>但同时为了简便，同时删除了URL里的分类信息，最近准备设置多个分类并把分类列表请回来，才发现设置多级目录后文章分类一直有问题，并不是我在<tt class="docutils literal">PATH_METADATA</tt>里指定的第一级目录，而是和文章的slug一样是最后一级目录。</p>
<p>检查后发现是<tt class="docutils literal">USE_FOLDER_AS_CATEGORY</tt>导致了<tt class="docutils literal">PATH_METADATA</tt>失效，只要没有禁用<tt class="docutils literal">USE_FOLDER_AS_CATEGORY</tt>，Pelican就会在先将路径最后一级目录的名称设置为<tt class="docutils literal">category</tt>，并忽略后面任何设置<tt class="docutils literal">category</tt>的尝试。将<tt class="docutils literal">USE_FOLDER_AS_CATEGORY</tt>设为<tt class="docutils literal">False</tt>就可以了。</p>
<p>最后总结一下，<tt class="docutils literal">PATH_METADATA</tt>、<tt class="docutils literal">FILENAME_METADATA</tt>和<tt class="docutils literal">USE_FOLDER_AS_CATEGORY</tt>可以这样配合使用：</p>
<div class="highlight"><pre><span></span><span class="n">PATH_METADATA</span> <span class="o">=</span> <span class="s1">&#39;(?P&lt;date&gt;\d</span><span class="si">{8}</span><span class="s1">)/(?P&lt;slug&gt;[^/]*)/[^/]*/[^/]*&#39;</span>  <span class="c1"># Ignore file name and the last folder</span>
<span class="n">USE_FOLDER_AS_CATEGORY</span> <span class="o">=</span> <span class="kc">True</span>  <span class="c1"># Default</span>
<span class="n">FILENAME_METADATA</span> <span class="o">=</span> <span class="s1">&#39;(?P&lt;lang&gt;\w*)&#39;</span>  <span class="c1"># No file extension</span>
</pre></div>
<p>2016年10月13日更新：</p>
<p>我提交了一个<a class="reference external" href="https://github.com/getpelican/pelican/pull/2015">Pull Request</a>，其目的之一是防止<tt class="docutils literal">USE_FOLDER_AS_CATEGORY</tt>覆盖<tt class="docutils literal">PATH_METADATA</tt>或<tt class="docutils literal">FILENAME_METADATA</tt>，现在它已被合并到主线中。</p>
<p>（完）</p>

    </div>
<nav class="nav sidebar">
    <div class="siteinfo">
        <div class="sitename"><a href="../..">coinfaces</a></div>
        <div class="tagline">Another Side of Life</div>
    </div>

        <a href="../../categories/">categories</a>
        <a href="../..">&larr; Home</a>
</nav><footer class="footer">
    <span>
        &copy; jpli &middot;
        under <a rel="license" href="http://creativecommons.org/licenses/by-sa/4.0/">CC-BY-SA 4.0</a>
    </span>
    |
    <span>
        Powered by:
        <a href="http://blog.getpelican.com/">Pelican</a>
    </span>
</footer></body>
</html>